<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('department_positions', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 部门信息
            $table->unsignedBigInteger('department_id');
            $table->string('department_name', 100)->nullable();
            $table->string('department_code', 50)->nullable();

            // 岗位信息
            $table->unsignedBigInteger('position_id');
            $table->string('position_name', 100)->nullable();
            $table->string('position_code', 50)->nullable();

            // 岗位配置
            $table->integer('standard_headcount'); // 标准人数
            $table->integer('minimum_headcount')->nullable(); // 最低人数
            $table->integer('maximum_headcount')->nullable(); // 最高人数
            $table->integer('current_headcount')->default(0); // 当前人数

            // 岗位要求
            $table->string('required_level', 50)->nullable(); // 要求等级
            $table->string('required_education', 50)->nullable(); // 学历要求
            $table->integer('required_experience_years')->nullable(); // 工作经验要求
            $table->json('required_skills')->nullable(); // 技能要求
            $table->json('responsibilities')->nullable(); // 岗位职责

            // 薪资范围
            $table->decimal('min_salary', 10, 2)->nullable(); // 最低薪资
            $table->decimal('max_salary', 10, 2)->nullable(); // 最高薪资
            $table->string('salary_currency', 10)->default('CNY'); // 薪资货币

            // 状态管理
            $table->string('status', 20)->default('active'); // active, inactive, frozen
            $table->boolean('is_key_position')->default(false); // 是否关键岗位
            $table->boolean('allow_overstaffing')->default(false); // 是否允许超编

            // 预警设置
            $table->integer('warning_threshold')->default(80); // 预警阈值（百分比）
            $table->boolean('enable_warning')->default(true); // 启用预警
            $table->string('warning_level', 20)->default('medium'); // low, medium, high

            // 统计信息
            $table->integer('vacancy_count')->default(0); // 空缺数量
            $table->decimal('occupancy_rate', 5, 2)->nullable(); // 占用率
            $table->datetime('last_updated')->nullable(); // 最后更新时间

            // 备注和附件
            $table->text('notes')->nullable();
            $table->json('attachments')->nullable();

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['department_id', 'position_id'], 'dp_dept_pos_idx');
            $table->index(['department_id'], 'dp_dept_idx');
            $table->index(['position_id'], 'dp_pos_idx');
            $table->index(['status'], 'dp_status_idx');
            $table->index(['is_key_position'], 'dp_key_idx');
            $table->index(['warning_level'], 'dp_warning_idx');
            $table->index(['occupancy_rate'], 'dp_occupancy_idx');

            // 外键约束
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('cascade');
            $table->foreign('position_id')->references('id')->on('positions')->onDelete('cascade');

            // 唯一约束：每个部门下的每个岗位只能有一条配置记录
            $table->unique(['department_id', 'position_id'], 'unique_dept_position');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('department_positions');
    }
};
